<template>
	<view>
		<u-cell-group>
			<u-cell title="名字" :value="userInfo.name" :isLink="true" @click="toUpName"></u-cell>
			<u-picker :show="showSexpic" @confirm="sureSex" :columns="sexs" @cancel='toShowSexPic'></u-picker>
			<u-cell title="性别" :value="userInfo.sex" :isLink="true" @click="toShowSexPic"></u-cell>
			<u-cell title="生日" :value="userInfo.birthday" :isLink="true" @click="showDatePick"></u-cell>
			<u-datetime-picker :show="showDatePic" v-model="value1" mode="date" @confirm="sureBirthday"
				:minDate="-2209017600">
			</u-datetime-picker>
			<u-cell title="修改密码" :isLink="true" @click="toUpdatePass"></u-cell>
		</u-cell-group>
	</view>
</template>

<script>
	import {
		upSex,
		upBirthday
	} from '@/config/api.js'
	export default {
		data() {
			return {
				showDatePic: false,
				value1: Number(new Date()),
				sexs: [
					['男', '女']
				],
				userInfo: {
					name: '',
					sex: '',
					birthday: '',
				},
				showSexpic: false,
				name: ''

			}
		},
		onShow() {
			//读取数据库中用户信息进行展示let name = null;
			let token = null;

			let name = null;

			uni.getStorage({
				key: 'user_name',
				success: function(res) {
					name = res.data
				}
			});
			uni.getStorage({
				key: 'user_token',
				success: function(res) {
					token = res.data
				}
			});
			uni.$u.http.get(`/serviceUser/user/info/${name}`, {
				params: {
					token: token
				}
			}).then((res) => {
				this.userInfo.name = res.info.userNickname;
				this.userInfo.sex = res.info.userSex;
				this.userInfo.birthday = res.info.userBirthday
			}).catch((error) => {
				console.log(error)
			})
		},
		onLoad() {
			//读取数据库中用户信息进行展示let name = null;
			let token = null;

			let name = null;

			uni.getStorage({
				key: 'user_name',
				success: function(res) {
					name = res.data
				}
			});
			uni.getStorage({
				key: 'user_token',
				success: function(res) {
					token = res.data
				}
			});
			uni.$u.http.get(`/serviceUser/user/info/${name}`, {
				params: {
					token: token
				}
			}).then((res) => {
				this.userInfo.name = res.info.userNickname;
				this.userInfo.sex = res.info.userSex;
				this.userInfo.birthday = res.info.userBirthday
			}).catch((error) => {
				console.log(error)
			})

		},
		methods: {
			sureBirthday(e) {
				//确定生日将生日转换格式
				this.userInfo.birthday = uni.$u.timeFormat(e.value, 'yyyy-mm-dd')
				let name = '';
				uni.getStorage({
					key: 'user_name',
					success: function(res) {
						name = res.data
					}
				})
				upBirthday({}, {
					params: {
						name: name,
						newBirthday: this.userInfo.birthday
					}
				}).then((r) => {
					console.log(r);
				}).catch((e) => {
					console.log(e);
				})
				console.log(this.userInfo.birthday);
				this.showDatePick()
			},
			showDatePick() {
				this.showDatePic = !this.showDatePic
			},
			toUpName() {
				uni.navigateTo({
					url: 'upName'
				})
			},
			toUpdatePass() {
				uni.navigateTo({
					url: 'upPass'
				})
			},
			toShowSexPic() {
				this.showSexpic = !this.showSexpic
			},
			sureSex(e) {
				this.userInfo.sex = e.value[0]
				let name = '';
				uni.getStorage({
					key: 'user_name',
					success: function(res) {
						name = res.data
					}
				})
				upSex({}, {
					params: {
						name: name,
						newSex: this.userInfo.sex
					}
				}).then(res => {
					console.log(res)
				}).catch(e => {
					console.log(e);
				})
				this.showSexpic = false


			}
		}
	}
</script>

<style>

</style>
